package com.xmz.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmz.entity.SClass;
import com.xmz.entity.Student;
import com.xmz.mapper.ClassMapper;
import com.xmz.mapper.StudentMapper;
import com.xmz.service.StudentService;
import com.xmz.util.PageResult;
import com.xmz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private  StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;
    public PageResult<Student> loadList(Integer pageNo, Integer pageSize, StudentVo student){
        IPage<Student> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Student> Wrapper = new QueryWrapper<>();
        if(student != null){
            Wrapper.like(StrUtil.isNotBlank(student.getName()),"name",student.getName());
            Wrapper.eq(student.getAge() !=null,"age",student.getAge());
            Wrapper.eq(StrUtil.isNotBlank((student.getSclass())),"sclass",student.getSclass());
        }
        IPage<Student> result = studentMapper.selectPage(page,Wrapper);
        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setRows(result.getRecords());
        pageResult.setTotal(result.getTotal());
        return  pageResult;
    }
    public List<SClass> loadAllClass(){
        return  classMapper.selectList(null);
    }
    @Override
    public int addStudent(Student student) {
        return studentMapper.insert(student);
    }
    @Override
    public int deleteStudent(Integer id) {
        return  studentMapper.deleteById(id);
    }
}
